package com.pureut.order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.order.dto.DetailViewDto;
import com.pureut.order.dto.SalesOrderDetailDto;
import com.pureut.order.dto.SalesOrderDto;
import com.pureut.order.service.SalesOrderDetailService;
import com.pureut.order.vo.GenerateInvoiceVo;
import com.pureut.order.vo.SalesOrderDetailVo;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/***
 * 销售明细列表
 * Author:C
 * Date:2022/12/01 14:59
 * @DESC
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sales-order-detail")
public class SalesOrderDetailController extends BaseController {


    private final SalesOrderDetailService salesOrderDetailService;


    /**
     * 获取销售订单明细列表
     *
     * @param id
     * @return
     */
    @GetMapping("/list/{id}")
    public TableDataInfo<SalesOrderDetailDto> list(@PathVariable("id") Long id, PageQuery pageQuery) {
        //查询销售订单列表
        return salesOrderDetailService.getList(id,pageQuery);
    }

    /**
     * 查看明细
     */
    @GetMapping("/view/{id}")
    public R<SalesOrderDetailDto> view(@PathVariable("id") Long id) {
        return R.ok(salesOrderDetailService.getView(id));
    }

    /**
     * 修改
     */
    @Log(title = "修改", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Void> update(@RequestBody SalesOrderDetailVo salesOrderDetailVo) {
        return toAjax(salesOrderDetailService.updateSalesOrderDetail(salesOrderDetailVo));
    }

    /**
     * 根据订单id和物料编码查询销售明细
     */
    @GetMapping("/getDetailById")
    public R<DetailViewDto> getDetailById(Long salesOrderId, String materialCode) {
        return R.ok(salesOrderDetailService.getDetailById(salesOrderId, materialCode));
    }


    /**
     * 生成发货单
     */
    //@SaCheckPermission("order:sale:saleOrder:invoice")
    @PostMapping("/generateInvoice")
    @Log(title = "生成发货单", businessType = BusinessType.INSERT)
    public R<?> generateInvoice(@RequestBody GenerateInvoiceVo generateInvoiceVo) throws Exception {
        return R.ok(salesOrderDetailService.generateInvoice(generateInvoiceVo));
    }

    /**
     * 根据物料编码获取销售订单
     */
    @GetMapping("/getOrderByMaterialCode")
    public R<List<SalesOrderDto>> getOrderByMaterialCode(String materialCode) {
        return R.ok(salesOrderDetailService.getOrderByMaterialCode(materialCode));
    }


}
